package com.hy.controller;

import com.hy.bean.Goods;
import com.hy.service.GoodsService;
import com.hy.service.impl.GoodsServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * (Goods)表控制层
 *
 * @author makejava
 * @since 2024-04-29 11:08:29
 */
@RestController
@RequestMapping("goods")
public class GoodsController {
    /**
     * 服务对象
     */

    private GoodsService goodsService = new GoodsServiceImpl();

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @RequestMapping("select")
    public ResponseEntity<List<Map<String, Object>>> select(String name) throws Exception {
        return ResponseEntity.ok(this.goodsService.selectAll(name));
    }

    @GetMapping("queryAll")
    public List<Goods> queryAll(){
        return goodsService.queryAll();
    }

    /**
     * 新增数据
     *
     * @param goods 实体
     * @return 新增结果
     */
    @RequestMapping("insert")
    public ResponseEntity<Goods> add(Goods goods, HttpServletRequest request) throws Exception {
        Long id = (Long) request.getSession().getAttribute("id");
        goods.setRecordUserId(id);
        return ResponseEntity.ok(this.goodsService.insert(goods));
    }

    /**
     * 编辑数据
     *
     * @param goods 实体
     * @return 编辑结果
     */
    @RequestMapping("update")
    public ResponseEntity<Goods> edit(Goods goods, HttpServletRequest request) throws Exception {
        Long id = (Long) request.getSession().getAttribute("id");
        goods.setRecordUserId(id);
        return ResponseEntity.ok(this.goodsService.update(goods));

    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("delete")
    public ResponseEntity<Boolean> deleteById(int id) throws Exception {
        return ResponseEntity.ok(this.goodsService.deleteById(id));
    }

}

